﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace IMark.DAO
{
    public class LoaiBaiVietModels
    {
        public static List<LOAI_BAI_VIET> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.LOAI_BAI_VIET.ToList();
            }
        }
        public static LOAI_BAI_VIET GetByID(int ID)
        {
            using (var context = new EMartEntities())
            {
                return context.LOAI_BAI_VIET.Find(ID);
            }
        }
        public static bool Add(LOAI_BAI_VIET loaiBaiViet)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    context.LOAI_BAI_VIET.Add(loaiBaiViet);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Update(LOAI_BAI_VIET loaiBaiViet)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_BAI_VIET existing = context.LOAI_BAI_VIET.Find(loaiBaiViet.ID);
                    existing.Ten = loaiBaiViet.Ten;
                    existing.MoTa = loaiBaiViet.MoTa;
                    existing.ThuTu = loaiBaiViet.ThuTu;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Delete(int ID)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_BAI_VIET existing = context.LOAI_BAI_VIET.Find(ID);
                    context.LOAI_BAI_VIET.Remove(existing);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static int GetCurrentOrdinal()
        {
            using(var context = new EMartEntities())
            {
                try
                {
                    return context.LOAI_BAI_VIET.Max(u => u.ThuTu);
                }
                catch (Exception)
                {
                    return 0;                    
                }
            }
        }

        public static string SelectNewsType(int IDLoaiBaiViet=0)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<select class='form-control' id='newsType' name='newsType'>");
            foreach (var loaiBaiViet in GetAll())
            {
                if(IDLoaiBaiViet == loaiBaiViet.ID)
                    result.Append("<option selected=\"selected\" value='" + loaiBaiViet.ID + "'>");
                else
                    result.Append("<option value='" + loaiBaiViet.ID + "'>");
                result.Append(loaiBaiViet.Ten);
                result.Append("</option>");                
            }
            result.Append("</select>");
            return result.ToString();
        }
    }
}